<?php

namespace App\resources\Api;

use App\Enum\ActivityEnum;
use Illuminate\Http\Resources\Json\JsonResource;

class GetAtivityCollection extends JsonResource
{
    public function toArray($request)
    {
        $resource = $this->resource;

        $resource->status = $resource->state;
        $resource->status_name = ActivityEnum::STATUS_NAMES[$resource->status];

        $currentTime = time();
        $startTime = $resource->start_time ? strtotime($resource->start_time) : 0;
        $endTime = $resource->end_time ? strtotime($resource->end_time) : 0;

        if( $resource->status == ActivityEnum::STATUS_PASS ) {
            if ($currentTime < $startTime) {
                $resource->status_name = "未开始";
            } elseif ($currentTime > $endTime) {
                $resource->status_name = "已结束";
            } else {
                $resource->status_name = "进行中";
            }
        }
        return [
            'id' => $resource->id,
            'title' => $resource->title ?? '',
            'description' => $resource->description ?? '',
            'start_time' => $resource->start_time ? date('Y.m.d', strtotime($resource->start_time)) : '',
            'end_time' => $resource->end_time ? date('Y.m.d', strtotime($resource->end_time)) : '',
            'status_name' => $resource->status_name,
            'location' => $resource->location?? '',
            'image_url' => $resource->image_url
        ];
    }

}
